//
//  SINAPhotoPopView.m
//  sinashebao
//
//  Created by  androidlongs on 2017/12/28.
//  Copyright © 2017年  androidlongs. All rights reserved.
//

#import "SINAPhotoPopView.h"


@interface  SINAPhotoPopView()

@property(strong,nonatomic) UILabel *savePhotoUILabel;
@property(strong,nonatomic) UILabel *selectPhotoUILabel;
@property(strong,nonatomic) UILabel *createPhotoUILabel;
@property(strong,nonatomic) UILabel *canlePhotoUILabel;
@end
@implementation SINAPhotoPopView

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */
- (instancetype)init
{
    self = [super init];
    if (self) {
        
        
        self.bounds = [UIScreen mainScreen].bounds;
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.64];
        self.layer.opacity = 0.0;
        
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disMiss)];
        self.userInteractionEnabled = YES;
        [self addGestureRecognizer:tap];
        
        [self addSubview:self.contentView];
        
        self.contentView.frame = CGRectMake(self.contentView.frame.origin.x, self.contentView.frame.origin.y, self.contentView.frame.size.width, 200);
        
        UILabel *createPhotoUILabel = [[UILabel alloc]initWithFrame:CGRectMake(0,5, mainW, 44)];
        createPhotoUILabel.text=@"拍照";
        
        createPhotoUILabel.textAlignment=NSTextAlignmentCenter;
        createPhotoUILabel.textColor=[UIColor colorWithHexString:@"#212121"];
        self.createPhotoUILabel=createPhotoUILabel;
        UIView *createDiviView = [[UIView alloc]initWithFrame:CGRectMake(0, 51, mainW, 1)];
        [createDiviView setBackgroundColor:[UIColor colorWithHexString:@"#eeeeee"]];
        
        // 1. 创建一个点击事件，点击时触发labelClick方法
        UITapGestureRecognizer *createTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(createPhoneFucntion)];
        // 2. 将点击事件添加到label上
        [createPhotoUILabel addGestureRecognizer:createTapGestureRecognizer];
        // 可以理解为设置label可被点击
        createPhotoUILabel.userInteractionEnabled = YES;
        
        [self.contentView addSubview:createDiviView];
        [self.contentView addSubview:createPhotoUILabel];
        
        
        UILabel *selectPhotoUILabel = [[UILabel alloc]initWithFrame:CGRectMake(0,55, mainW, 44)];
        selectPhotoUILabel.text=@"从手机相册选择";
        
        selectPhotoUILabel.textAlignment=NSTextAlignmentCenter;
        selectPhotoUILabel.textColor=[UIColor colorWithHexString:@"#212121"];
        self.selectPhotoUILabel=selectPhotoUILabel;
        UIView *selectDiviView = [[UIView alloc]initWithFrame:CGRectMake(0, 101, mainW, 1)];
        [selectDiviView setBackgroundColor:[UIColor colorWithHexString:@"#eeeeee"]];
        
        // 1. 创建一个点击事件，点击时触发labelClick方法
        UITapGestureRecognizer *selectTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectPhoneFucntion)];
        // 2. 将点击事件添加到label上
        [selectPhotoUILabel addGestureRecognizer:selectTapGestureRecognizer];
        // 可以理解为设置label可被点击
        selectPhotoUILabel.userInteractionEnabled = YES;
        
        [self.contentView addSubview:selectDiviView];
        [self.contentView addSubview:selectPhotoUILabel];
        
        UILabel *savePhotoUILabel = [[UILabel alloc]initWithFrame:CGRectMake(0,105, mainW, 44)];
        savePhotoUILabel.text=@"保存到相册";
        
        savePhotoUILabel.textAlignment=NSTextAlignmentCenter;
        savePhotoUILabel.textColor=[UIColor colorWithHexString:@"#212121"];
        
        self.savePhotoUILabel = savePhotoUILabel;
        
        UIView *saveDiviView = [[UIView alloc]initWithFrame:CGRectMake(0, 152, mainW, 1)];
        [saveDiviView setBackgroundColor:[UIColor colorWithHexString:@"#eeeeee"]];
        
        // 1. 创建一个点击事件，点击时触发labelClick方法
        UITapGestureRecognizer *saveTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(savePhoneFucntion)];
        // 2. 将点击事件添加到label上
        [savePhotoUILabel addGestureRecognizer:saveTapGestureRecognizer];
        // 可以理解为设置label可被点击
        savePhotoUILabel.userInteractionEnabled = YES;
        
        [self.contentView addSubview:saveDiviView];
        [self.contentView addSubview:savePhotoUILabel];
        
        
        [self.contentView addSubview:selectPhotoUILabel];
        
        UILabel *canlePhotoUILabel = [[UILabel alloc]initWithFrame:CGRectMake(0,150, mainW, 44)];
        canlePhotoUILabel.text=@"取消";
        self.canlePhotoUILabel=canlePhotoUILabel;
        canlePhotoUILabel.textAlignment=NSTextAlignmentCenter;
        canlePhotoUILabel.textColor=[UIColor colorWithHexString:@"#212121"];
        
        // 1. 创建一个点击事件，点击时触发labelClick方法
        UITapGestureRecognizer *cancleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disMiss)];
        // 2. 将点击事件添加到label上
        [canlePhotoUILabel addGestureRecognizer:cancleTapGestureRecognizer];
        // 可以理解为设置label可被点击
        canlePhotoUILabel.userInteractionEnabled = YES;
        
        
        [self.contentView addSubview:canlePhotoUILabel];
        
        
    }
    return self;
}
//选择拍照图片
-(void)createPhoneFucntion{
    [self disMiss];
    if (self.creatPhopeBlock) {
        self.creatPhopeBlock();
    }
}
//选择相册图片
-(void)selectPhoneFucntion{
    [self disMiss];
    if (self.selectPhopeBlock) {
        self.selectPhopeBlock();
    }
}
//保存图片
-(void)savePhoneFucntion{
    [self disMiss];
    if (self.savePhopeBlock) {
        self.savePhopeBlock();
    }
}

-(void)setIsShowSaveImage:(BOOL)isShowSaveImage{
    _isShowSaveImage=isShowSaveImage;
    if (isShowSaveImage) {
        //隐藏
        self.contentView.frame = CGRectMake(self.contentView.frame.origin.x, self.contentView.frame.origin.y, self.contentView.frame.size.width, 200-44);
        self.savePhotoUILabel.hidden=YES;
        self.canlePhotoUILabel.frame=CGRectMake(0,150-44, mainW, 44);
        
    }
}

- (UIView *)contentView
{
    if (!_contentView) {
        _contentView = [[UIView alloc]initWithFrame:CGRectMake(0, mainH, mainW,200)];
        [_contentView setBackgroundColor:[UIColor whiteColor]];
    }
    return _contentView;
}
//显示选择器
- (void)show
{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [self setCenter:[UIApplication sharedApplication].keyWindow.center];
    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
    
    
    CGRect frame =  self.contentView.frame;
    frame.origin.y -= self.contentView.frame.size.height;
    [UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        [self.layer setOpacity:1.0];
        self.contentView.frame = frame;
        
    } completion:^(BOOL finished) {
    }];
    
}

//移除选择器
- (void)disMiss
{
    
    CGRect frame =  self.contentView.frame;
    frame.origin.y += self.contentView.frame.size.height;
    [UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        [self.layer setOpacity:0.0];
        self.contentView.frame = frame;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}
@end
